// pages/youXuan/tabBar/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [
      {name:'热卖',id:1},
      {name:'美味餐桌',id:2},
      {name:'蔬菜',id:3},
      {name:'酒饮',id:4},
      {name:'水果',id:5},
      {name:'粮油',id:6},
      {name:'粮油',id:7},{name:'粮油',id:8}
    ],
    tabIndex:1,
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],//轮播图
    indicatorDots: true,//轮播小圆点显示
    autoplay: true,//轮播自动播放
    interval: 3000,//自动切换时间
    time:{
      leftd:'00',
      lefth:'00',
      leftm:'00',
      lefts:'00'
    },//限时秒杀
    timer:null,
    activeIdx:0,
    shopList:[],
    total:8,
    shopcartList:[]
  },
  goSearch(){
    wx.navigateTo({
      url: '/pages/youXuan/searchResult/index',
    })
  },
  getShopCart(){
    let list=wx.getStorageSync('yx-shopcart')||[]
    this.setData({
      shopcartList:list
    })

  },
  changeTab(event){
    //切换tab
    var dataset=event.currentTarget.dataset
    this.setData({
      tabIndex:dataset.id
    })
  },
  setTime(){
    var nowtime = new Date(),  //获取当前时间
      endtime = new Date("2021/2/17 21:42");  //定义结束时间
    var lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
      leftd = Math.floor(lefttime/(1000*60*60*24)),  //计算天数
      lefth = Math.floor(lefttime/(1000*60*60)%24),  //计算小时数
      leftm = Math.floor(lefttime/(1000*60)%60),  //计算分钟数
      lefts = Math.floor(lefttime/1000%60);  //计算秒数
      leftd=leftd>9?leftd:'0'+leftd
      lefth=lefth>9?lefth:'0'+lefth
      leftm=leftm>9?leftm:'0'+leftm
      lefts=lefts>9?lefts:'0'+lefts
      if(lefttime<=0){
        clearInterval(this.data.timer)
        return false
      }
      this.setData({
        time:{
          leftd,
          lefth,
          leftm,
          lefts
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //限时秒杀--定时器开始
    let timer=setInterval (()=> {
      this.setTime()
    }, 1000);  //反复执行函数本身
    this.setData({
      timer
    })

    this.getShopCart()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if(!wx.getStorageSync('yx-shoplist')){
      let list=[
        {id:1,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾1',money:'20.88',num:0},
        {id:2,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾2',money:'20.88',num:0},
        {id:3,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾3',money:'20.88',num:0},
        {id:4,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾4',money:'20.88',num:0}
      ]
      wx.setStorageSync('yx-shoplist',list) 
    }
    this.setData({
      shopList:wx.getStorageSync('yx-shoplist')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.total>this.data.shopList.length){
      setTimeout(()=>{
        let arr=[
          {id:5,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾1',money:'20.88',num:0},
          {id:6,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾2',money:'20.88',num:0},
          {id:7,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾3',money:'20.88',num:0},
          {id:8,name:'【今日必抢】本色纸巾纸巾纸巾纸巾纸巾纸巾4',money:'20.88',num:0}
        ]
        let data=this.data.shopList.concat(arr)
        this.setData({
          shopList:data
        })
      },1000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})